﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="${root!}/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${root!}/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${root!}/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/core/base.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerForm.js"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
<script src="${root!}/lib/jquery-validation/jquery.validate.min.js"></script>
<script src="${root!}/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${root!}/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${root!}/lib/js/orders.js"></script>
<script type="text/javascript" src="/lib/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="/lib/js/ajaxupload.3.6.js"></script>
<script> 
var groupicon = "${root!}/lib/ligerUI/skins/icons/communication.gif";
var archives = '${root!}/lib/ligerUI/skins/icons/archives.gif';
var t = null;
var g= null;

var f = null;
var m = null; 
var r = null;
$(function (){
	/**菜品状态*/
	var status = [{value : "0", text : "不销售"},
	              {value : "1", text : "销售中"},
	              {value : "2", text : "售完"}];
	
	/** 页面布局 */	
	$(function (){
		$("#layout1").ligerLayout({ leftWidth: 200});
	});
	
	 //菜系树
	t = $("#tree").ligerTree({
		isExpand : 2,
		checkbox: false,
		slide: false,
		nodeWidth: 120,
		url:'${root!}/sys/cuisine/getCuisineTree',
		ajaxType: 'get', 
		textFieldName : 'text',
		cuisineIdFieldName : 'cuisine_id',
		onContextmenu:function (node, e)
            { 
                actionNodeID = node.data.text;
                menu.show({ top: e.pageY, left: e.pageX });
                return false;
            },
		onSelect : function(e) {
			//查询对应菜品
			if (e.data.cuisine_id != null) {
				//用户选择ID传给grid
				var y=[];
				y.push({name: "cuisine_id", value: e.data.cuisine_id});
				g.set('parms', y);
				g.loadData();
			}
		}
	});
	
	/**树右键菜单*/
	var menu = $.ligerMenu({ top: 100, left: 100, width: 120, items:
	   [{ text: '刷新', click: f_Reload ,icon:'refresh'}]
	});
	
	
	 /**菜品信息显示*/
	 $("#navtab1").ligerTab();
	 /**菜品工具行*/
	 $("#toptoolbar").ligerToolBar({ items: [
             		{ text: '增加', click: f_New ,icon: 'add' },
	   				{ line: true },
					{ text: '修改', click: f_Modify ,icon:'modify'},
					{ line: true },
					{ text: '删除', click: f_Del ,icon:'delete'}
             	]});

	 g = $("#grid2").ligerGrid({
			columns: [
			  { display: '菜品ID', name: 'DISH_ID', align: 'center', width: 120, hide: 0} ,
			  { display: '序号', name: 'SQ',align: 'center',width: 120},
			  { display: '菜品名', name: 'DISH_NAME', align: 'center',width: 120} ,
				 { display: "价格", name: "PRICE", align: 'center',width: 120},	
			  { display: '图片', name: 'IMAGE', align: 'center',width: 120 } ,
			  { display: '简介', name: 'INFO', align: 'center',width: 120} ,
			  { display: '查看次数', name: 'VIEWCOUNT', align: 'center',width: 120} ,
			  { display: '状态', name: 'STATUS', align: 'center',width: 120, 
				  render:function(rowdata, index, value){
					  if(rowdata.STATUS=='1')
						  return "销售中";
					  else if(rowdata.STATUS == '0')
						  return "待销售";
					  else
						  return rowdata.STATUS == '2'? '售完' : rowdata.STATUS;
				 }
			  } ,
			  { display: '所属菜系ID', name: 'CUISINE_ID',align: 'center', width: 120, hide: 1} ,
			  { display: '所属菜系', name: 'CUISINE_NAME',align: 'center',width: 120} ,
			  { display: '最后更新人', name: 'LAST_UPDATED_BY',align: 'center',width: 120},
			  { display: '最后更新时间', name: 'LAST_UPDATE_DATE',align: 'center',width: 200}
			],
			url: '${root!}/sys/cuisineDish/getCuisineDishMessageJson',
			width: '100%', 
			height: '100%', 
			checkbox: false,
			allowUnSelectRow:true,
			method:"post",
			parms:[{name:"cuisine_id",value:'0'}]
		});

		
		/**菜品新增*/
		m = $("#DishForm").ligerForm({
			 inputWidth: 150, labelWidth: 80, space: 50,show:false,validate:{},
				 fields: [
				 { display: "菜系ID", name: "CUISINE_ID", type: "text", width: 120, type: "hidden" },
				 { display: "菜品ID", name: "DISH_ID", type: "text", width: 120, type: "hidden"  },
				 { display: "序列", name: "SQ", newline: true, type: "number", width: 120,validate:{required:true}, group: "菜单信息",groupicon: groupicon},
				 { display: "名称", name: "NAME", newline: false, type: "text", width: 120,validate:{required:true}},
				 { display: "价格", name: "PRICE", newline: true, type: "number", width: 120,validate:{required:true}},		 
				 { display: "状态", name: "STATUS", newline: false, type: "text", width: 120,validate:{required:true},type: "combobox", 
						options : {
							data : status,
							valueField : 'value',
							textField : 'text',
							value : '0'
						}	 
				 },		 
				 { display: "简介", name: "INFO", newline: true, type: "text", width: 120,validate:{required:true}}
				,{ display: "选择图片", value:'2', id:"IMAGE", name:"IMAGE", comboboxName:"UPLOAD_", type: "text"} 
				 ]
			 });  
		//初始化时隐藏
		$("#DishForm").hide();
		//$.ligerui.get("UPLOAD_").set('onBeforeOpen', "toUpload()");
		
});


/**新增菜品*/
function f_New()
{
	DishClear();
	var data = t.getSelected();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请选择菜系后再新增。');
	 	return;
	} 	
	if(data['data']['cuisine_id'] == 0) {//选中所有菜系	
		$.ligerDialog.success('温馨提示：不能在所有菜系下新增菜品，请选择具体菜系。');
	 	return;
	}else if(data['data']['cuisine_id'] != 0){ //菜品新增
		f_DishCreateForm();
	}
}

/** 菜品创建form*/
function f_DishCreateForm(){
	var filtercontainer = $("#DishForm").width(530).height(200).hide();
		var selected = t.getSelected();
		//设置菜系ID
		m.setData({ CUISINE_ID: selected['data']['cuisine_id']});
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'菜品新增',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Dish_Save_Update()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) {  DishClear(); dialog.hide(); } }
			 ]
		});
}


/**菜品保存*/
function f_Dish_Save_Update(){
	var success = false;
	var data = m.getData();
	var jsonStr = [];
	if (m.valid()) {
		do_ajaxSubmit2('', 'DishForm', '${root!}/sys/dish/saveOrUpdate',jsonStr);
		//var param = "json="+$.ligerui.toJSON(data);
		f_Reload();
		DishClear();
		g.reload();
		success = true;
	} else {
		m.showInvalid();
	}
	return success;
}

/**菜单保存后清空*/
function DishClear(){
	var initData = {"CUISINE_ID":"","DISH_ID":"","PRICE":"","IMAGE":"","INFO":"","NAME":"","SQ":""};
	m.setData(initData);
}


/**菜单修改*/
function f_Modify(){	
	var data = t.getSelected();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请选择菜系后再修改。');
	 	return;
	} 
	var data = g.getSelectedRow();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请选择要修改的行。');
	 	return;
	} 
	var filtercontainer = $("#DishForm").width(530).height(200).hide();
	var selected = t.getSelected();
		//设置要修改的内容
		m.setData({ CUISINE_ID: selected['data']['cuisine_id'],
					DISH_ID: data['DISH_ID'],
					SQ: data['SQ'],
					NAME: data['DISH_NAME'],
					PRICE: data['PRICE'],
					STATUS: data['STATUS'],
					INFO: data['INFO']
				}); 
		//$.ligerui.get("UPLOAD_").setValue(data['IMAGE']);
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'菜品修改',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Dish_Save_Update()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) { DishClear(); dialog.hide(); } }]
		});
}


/** 菜品删除*/
function f_Del(){
	var row = g.getSelectedRow();
	if (!row) {
		$.ligerDialog.success('温馨提示：请选择要删除的行。');
		return; 
	}
	var jsonStr = [];
	m.setData({CUISINE_ID: row['CUISINE_ID'],DISH_ID: row['DISH_ID']});
	$.ligerDialog.confirm('确认是否删除？', function (yes){
		if(yes==true){
			do_ajaxSubmit2('', 'DishForm', '${root!}/sys/dish/delete',jsonStr);
			f_Reload();
			g.reload();
		};
    });
}


/**重新加载树*/
function f_Reload(){
	//重新加载树
	t.clear();	
	t.loadData(null, '${root!}/sys/cuisine/getCuisineTree');
	//刷新菜品
	g.setParm("cuisine_id", 0);
	g.reload();

}

</script>
<style type="text/css"> 
	body{ padding:0px; margin:0; overflow:hidden;}
	#layout1{  width:100%;margin:0; padding:0;  }  
	.l-page-top{ height:80px; background:#f8f8f8; margin-bottom:3px;}
	.tree{
		width:200px;
		height:600px;
		margin: 0px;
		border: 1px solid #ccc;
		overflow: scroll;}
</style>
</head>
<body>
<div id="layout1">
	<div position="left" title="菜系">
		<div class="tree">
			<ul id="tree" style="margin-top:3px;"></ul>
		</div>
	</div>
	<div position="center" id="navtab1">
		<!--  <div title="角色信息">
			工具栏
			<div id="toptoolbar"></div>
			<div id="grid1"></div>
		</div> -->
		<div title="菜单信息">
			<div id="toptoolbar"></div>
			<div id="grid2"></div>
		</div>
	</div>
</div> 
<form id="ModuleForm"></form>
<form id="DishForm">
	<div id="upload_button">点击图片上传</div>
</form>
<form id="RoleForm"></form>
</body>
<script type="text/javascript">
    var button = $('#upload_button'), interval;
    var fileType = "pic",fileNum = "one"; 
    new AjaxUpload(button,{
        action: '/sys/dish/uploadImg',
        data:{
           // 'cuisine_id': $('#DishForm input[name="CUISINE_ID"]').val()
        },
        name: 'userfile',
        onSubmit : function(file, ext){
            if(fileType == "pic")
            {
                if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
                   /*  this.setData({
                        'info': '文件类型为图片'
                    }); */
                } else {
                	alert('非图片类型文件，请重传');
                    return false;               
                }
            }
            button.text('文件上传中');
            if(fileNum == 'one')
                this.disable();
            interval = window.setInterval(function(){
                var text = button.text();
                if (text.length < 14){
                    button.text(text + '.');                    
                } else {
                    button.text('文件上传中');             
                }
            }, 200);
        },
        onComplete: function(file, response){
        	response = response.replace("<pre>","").replace("</pre>","");
        	var obj = strToJson(response); 
            if(obj.msg == "success"){
            	//$('#IMAGE').val(obj.image);
            	$('#DishForm input[name="IMAGE"]').val(obj.image);
                button.text(file);       
            }else{
            	alert("上传失败，请稍后重试");
	            button.text('文件上传');
            }
            window.clearInterval(interval);
            this.enable();
        }
    });
    
    //字符串转json
    function strToJson(str){
	    var json = (new Function("return " + str))();
    	return json;
    }
</script>
</html>
